//
//  YMFLoginViewController.swift
//  FlyingPigeon
//
//  Created by yang on 2019/5/30.
//  Copyright © 2019年 a. All rights reserved.
//

import Foundation
import UIKit

class YMFLoginViewController: UIViewController {
    @IBOutlet weak var userName: UITextField!
    @IBOutlet weak var useNameView: UIView!
    
    @IBOutlet weak var userSecritView: UIView!
    @IBOutlet weak var userSecrit: UITextField!
    @IBOutlet weak var loginButton: UIButton!
    @IBOutlet weak var useIcon: UIImageView!
    
    @IBOutlet weak var customerButton: UIButton!
    @IBOutlet weak var keyboardIcon: UIImageView!
    override func viewDidLoad() {
        
        useNameView.layer.borderWidth = 1
        useNameView.layer.borderColor = UIColor.white.cgColor
        useNameView.layer.cornerRadius = useNameView.bounds.height * 0.5
        useNameView.clipsToBounds = true

        userSecritView.layer.borderWidth = 1
        userSecritView.layer.borderColor = UIColor.white.cgColor
        userSecritView.layer.cornerRadius = userSecritView.bounds.height * 0.5
        userSecritView.clipsToBounds = true
        userName.setValue(UIColor.white, forKeyPath: "_placeholderLabel.textColor")
//        userName.layer.borderWidth = 1
//        userName.layer.borderColor = UIColor.white.cgColor
//        userName.layer.cornerRadius = userName.bounds.height * 0.5
//        userName.clipsToBounds = true
        
        userSecrit.setValue(UIColor.white, forKeyPath: "_placeholderLabel.textColor")
//        userSecrit.layer.borderWidth = 1
//        userSecrit.layer.borderColor = UIColor.white.cgColor
//        userSecrit.layer.cornerRadius = userSecrit.bounds.height * 0.5
//        userSecrit.clipsToBounds = true
        
        userSecrit.isSecureTextEntry = true
        
        loginButton.layer.cornerRadius = loginButton.bounds.height * 0.5
        loginButton.clipsToBounds = true
        
        useIcon.image = UIImage(named: "userName_icon")?.withRenderingMode(.alwaysTemplate)
        useIcon.tintColor = UIColor.white
        
        keyboardIcon.image = UIImage(named: "keyboard")?.withRenderingMode(.alwaysTemplate)
        keyboardIcon.tintColor = UIColor.white
        
    }
    
    @IBAction func loginAction(_ sender: Any) {
        if userName.text == "1353829925" && userSecrit.text == "123456" {
            UserDefaults.standard.setValue("王一鸣", forKey: "userName")
            UserDefaults.standard.setValue("5年", forKey: "year")
            UserDefaults.standard.synchronize()
            
            UIApplication.shared.keyWindow?.rootViewController =  YMFMainTabController()
        } else {
            let alert = UIAlertController(title: "错误", message: "用户名或密码错误，请重新输入", preferredStyle: .alert)
            let action = UIAlertAction(title: "确定", style: .cancel, handler: { [weak alert] (action)   in
            alert?.dismiss(animated: true, completion: nil)
            })
            alert.addAction(action)
            present(alert, animated: true, completion: nil)
        }
    
    }
    
    @IBAction func customerAction(_ sender: Any) {
        
        UserDefaults.standard.setValue("游客", forKey: "userName")
        UserDefaults.standard.setValue("2年", forKey: "year")
        UserDefaults.standard.synchronize()
        
        UIApplication.shared.keyWindow?.rootViewController =  YMFMainTabController()
    }
}
